package voja.android.crvenazvezda;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {
	
	private String clocks[][] = {
		{ "Standard Alarm", "com.android.alarmclock", "com.android.alarmclock.AlarmClock" },
		{ "HTC Alarm ClockDT", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
		{ "Standard Alarm ClockDT", "com.android.deskclock", "com.android.deskclock.AlarmClock" },
		{ "Froyo Nexus Alarm ClockDT", "com.google.android.deskclock", "com.android.deskclock.DeskClock" },
		{ "Moto Blur Alarm ClockDT", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock" },
		{ "Samsung Galaxy S", "com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage" } 
	};
	private Boolean clockFound=false;
	
	private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
		public void onReceive(Context c, Intent intent) {
			int level = intent.getIntExtra("level", 0);
			
			RemoteViews v = new RemoteViews(c.getPackageName(), R.layout.widget);
			v.setTextViewText(R.id.txtBattery, String.valueOf(level) + "%");
		}
	};
	
	private BroadcastReceiver mClockReceiver = new BroadcastReceiver(){
		public void onReceive(Context c, Intent intent) {
			RemoteViews v = new RemoteViews(c.getPackageName(), R.layout.widget);
			v.setTextViewText(R.id.txtTime, Util.getTime());
			v.setTextViewText(R.id.txtDate, Util.getDate());
		}
	};
	
	public void onEnabled(Context context){
		Context appContext = context.getApplicationContext();
		appContext.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		appContext.registerReceiver(this.mClockReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
	}
	
	public void onReceive(Context context, Intent intent){
		String action = intent.getAction();
		if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)){
			
			// init Clock.onClick
			RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
			PendingIntent pIntent = PendingIntent.getActivity(context, 0, getAlarmIntent(context), 0);
			v.setOnClickPendingIntent(R.id.txtTime, pIntent);
			v.setTextViewText(R.id.txtTime, Util.getTime());
			v.setTextViewText(R.id.txtDate, Util.getDate());
			context.startService(new Intent(context, ClockReceiver.class));
			
			// init Time, Date and Battery Services
			v.setTextViewText(R.id.txtBattery, "loading...");
			context.startService(new Intent(context, BatteryReceiver.class));
			
			//init grb.OnClick
			PendingIntent pIntent2 = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
			v.setOnClickPendingIntent(R.id.imgCrvenaZvezda, pIntent2);
			
			AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), v);
		}
	}
	
	public Intent getAlarmIntent(Context context){
		PackageManager packageManager = context.getPackageManager();
		
		Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		ComponentName cn;
		
		for(int i=0; i<clocks.length; i++){
	        String packageName = clocks[i][1];
	        String className = clocks[i][2];
	        
	        try{
	        	cn = new ComponentName(packageName, className);
	        	packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
	        	intent.setComponent(cn);
	        	Log.v("WidgteProvider", "Clock found: " + packageName);
	        	clockFound = true;
	        	break;
	        }catch(Exception e){
	        	Log.e("WidgetProvider", "getAlarmIntent: " + e.getMessage());
	        }
		}
		
		return intent;
	}
}
